/**
	This file is part of MemoryTrace.

	MemoryTrace is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

        Written by Ameziane NAIT ABDELAZIZ.
        
	MemoryTrace is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with MemoryTrace.  If not, see <http://www.gnu.org/licenses/>.
**/

/* 
 * File:   BinaryReader.cpp
 * Author: maxds
 * 
 * Created on 10 août 2011, 17:22
 */

#include "BinaryReader.h"

BinaryReader::BinaryReader(string file_to_read, ILogger* transformer) {
    this->file_in=file_to_read;
    this->transformer=transformer;
}

BinaryReader::BinaryReader(const BinaryReader& orig) {
}

BinaryReader::~BinaryReader() {
    delete this->transformer;
}


#include <cassert>
#include <unistd.h>//read
#include <string.h>//memcpy
#include <sys/types.h>//fd
#include <fcntl.h>  //open

#include <sys/stat.h>
#include <errno.h>
#include "../include/alloc_event_t.h"

#define BUFFSIZE    100*EVENT_SIZE
void BinaryReader::transforme(){
  
  /*statistiques d'allocations*/
  alloc_stats st;
  st.memory_allocation=0;
  st.compteur_malloc=0;
  st.compteur_realloc=0;
  st.compteur_calloc=0;
  st.compteur_free=0;
  st.size=0;
  st.current_addr=0;
  st.compteur=0;
  st.nmemb=0;
  
  /*ouvre le fichier*/
  int in=open((this->file_in).c_str(), O_RDONLY);

#ifdef DEBUG
  printf("errno = %d\n",errno);
#endif
  
  assert(in>=0);
  char buff[BUFFSIZE];
  
  
  alloc_event_t* current_event;
  
  ssize_t lu=0;
  
  ssize_t ret=1;
  unsigned long int id=0;
  size_t memory_demanded=0;
  
  while(ret!=0){
  ret=read(in,buff,BUFFSIZE);
  lu+=ret;
  
  assert(ret !=-1);
  for(ssize_t i =0;i<ret;i+=EVENT_SIZE){
      current_event = (alloc_event_t*)(&buff[i]);
      
      //printf("id=%d, type=%c\n",id,current_event->type);
      switch(current_event->type){
        case 'c':
            id++;
	    
            transformer->startCalloc(current_event->ret,current_event->nmemb,current_event->size,id,memory_demanded);
            transformer->endElement("calloc");
	    
            
            /*maj des stats de calloc*/
	    st.memory_allocation+=current_event->nmemb*current_event->size;
            st.compteur_calloc++;
            st.compteur=id;
            st.nmemb=current_event->nmemb;
            
            break;
        case 'r':
	    id++;
            
            transformer->startRealloc(current_event->addr_realloc,current_event->ret,current_event->size,id,memory_demanded);
            transformer->endElement("realloc");
	    
            
            /*maj des stats de realloc*/
	    st.memory_allocation+=current_event->size;
            st.compteur_realloc++;
            st.compteur=id;
            break;
        case 'f':/*FREE*/
	    id++;
            
            transformer->startFree(current_event->ret,id);
            transformer->endElement("free");
	    
            st.compteur_free++;
            break;
        case 'm':/*MALLOC*/
	    id++;
            
            transformer->startMalloc(current_event->ret,current_event->size,id,memory_demanded);
            transformer->endElement("malloc");
            
            
            st.memory_allocation+=current_event->size;
            st.compteur_malloc++;
            st.compteur=id;
            break;

        default:
            id++;
            break;
    }
  }
 }
  
  close(in);
  transformer->finalizeLog(st);
  printf("Le nombre d'octets total lus à partir du fichier binaire: %d\n",lu);
}




